package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.dto.ArticleDTO;
import com.blog.entity.Article;
import com.blog.mapper.ArticleMapper;
import com.blog.service.ArticleService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Override
    public Page<Article> listArticles(Integer pageNum, Integer pageSize) {
        // 创建分页对象
        Page<Article> page = new Page<>(pageNum, pageSize);
        
        // 构建查询条件
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, 1); // 只查询已发布的文章
        queryWrapper.orderByDesc(Article::getCreateTime); // 按创建时间降序排序
        
        // 执行查询
        return this.page(page, queryWrapper);
    }

    @Override
    public Article getArticleDetail(Long id) {
        // 获取文章详情
        Article article = this.getById(id);
        
        // 更新浏览量
        if (article != null) {
            article.setViewCount(article.getViewCount() + 1);
            this.updateById(article);
        }
        
        return article;
    }

    @Override
    public boolean saveOrUpdateArticle(ArticleDTO articleDTO, Long userId) {
        Article article;
        
        if (articleDTO.getId() != null) {
            // 更新文章
            article = this.getById(articleDTO.getId());
            if (article == null) {
                throw new RuntimeException("文章不存在");
            }
        } else {
            // 新增文章
            article = new Article();
            article.setAuthorId(userId);
            article.setViewCount(0);
            article.setCreateTime(LocalDateTime.now());
        }
        
        // 设置文章属性
        article.setTitle(articleDTO.getTitle());
        article.setContent(articleDTO.getContent());
        article.setSummary(articleDTO.getSummary());
        article.setCoverImage(articleDTO.getCoverImage());
        article.setStatus(articleDTO.getStatus());
        
        // 添加分类ID
        article.setCategoryId(articleDTO.getCategoryId());
        
        article.setUpdateTime(LocalDateTime.now());
        
        return this.saveOrUpdate(article);
    }

    @Override
    public Page<Article> getArticlesByCategory(Long categoryId, Integer pageNum, Integer pageSize) {
        // 创建分页对象
        Page<Article> page = new Page<>(pageNum, pageSize);
        
        // 构建查询条件
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getCategoryId, categoryId); // 按分类ID查询
        queryWrapper.eq(Article::getStatus, 1); // 只查询已发布的文章
        queryWrapper.orderByDesc(Article::getCreateTime); // 按创建时间降序排序
        
        // 执行查询
        return this.page(page, queryWrapper);
    }
} 